#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2021/6/8
# @Author    :zhangxuchuo
# @Email     :zhangxuchuo@bbdservice.com
# @File      :EC72_UnsettledGuarantyBalance
# @Software  :PyCharm


'''eof
name:未结清借贷交易不良类余额
code:EC71_UnsettledDebtBadBalance
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import re
import sys
import datetime
import pandas as pd

# reload(sys)
# sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--', []]


def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def isFloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date

# 信用提示信息单元(EB01)·信贷交易提示信息段(EB01A)·担保交易余额(EB01AJ05)

def EC72_UnsettledGuarantyBalance():
    data = EC_DSJ_2D.get('data')
    #EBA 借贷账户信息 [1..1]
    EBA = getTable(data, 'EBA', 1, 1)
    if EBA == [{}]:
        return '缺失值'
    elif EBA == 0:
        return 0
    else:
        #EB01 信用提示信息单元 [0..1]
        EB01 = getTable(EBA[0], 'EB01', 1, 0)
        if EB01 == [{}]:
            return '缺失值'
        elif EB01 == 0:
            return 0
        else:
            #EB01A 信贷交易提示信息段 [0..1]
            EB01A = getTable(EB01[0], 'EB01A', 0, 1)
            if EB01A == [{}]:
                return '缺失值'
            elif EB01A == 0:
                return 0
            else:
                #EB01AJ05 担保交易余额
                EB01AJ04 = EB01A[0].get('EB01AJ04')
                if isFloat(EB01AJ04) and float(EB01AJ04) >= 0:
                    return EB01AJ04
                else:
                    return '缺失值'




result = EC72_UnsettledGuarantyBalance()
# if __name__ == '__main__':
#     with open(r'C:\Users\john\Desktop\samples\EC_DSJ_2D.json',encoding='utf8') as ff:
#         EC_DSJ_2D = json.loads(ff.read())
#     result = EC72_UnsettledGuarantyBalance()
#     print(result)